package com.sibu.yx.promotion.admin.web.support.interceptor;

import com.doubo.common.enums.AppName;
import com.doubo.common.threadlocal.SerialNo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.Set;

/**
 * 日志记录以及流水号初始化拦截器
 * Created with IntelliJ IDEA.
 * User: qiujingwang
 * Date: 2016/3/23
 * Description:
 */
@Slf4j
@Deprecated
public class SerialNoInitInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
     if (handler instanceof HandlerMethod) {
            SerialNo.init(AppName.DOUBO_BUYER_API);//设置流水号
            HandlerMethod handlerMethod = (HandlerMethod) handler;
            String clazzName = handlerMethod.getBeanType().getSimpleName();
            String methodName = handlerMethod.getMethod().getName();
            log.info("[{}] [{}.{}] start to handle request url:[{}] params:[{}]", SerialNo.getSerialNo(),
                    clazzName, methodName, request.getRequestURI(), mapToString(request.getParameterMap()));
        }
        return true;
    }

    private String mapToString(Map<String, String[]> paramMap) {
        StringBuilder stringBuilder = new StringBuilder(500);
        Set<Map.Entry<String, String[]>> entrySet = paramMap.entrySet();
        for (Map.Entry<String, String[]> entry : entrySet) {
            stringBuilder.append(entry.getKey());
            stringBuilder.append("=[");
            String[] values = entry.getValue();
            int len;
            if ((len = values.length) > 0) {
                for (int i = 0; i < len; ) {
                    stringBuilder.append(values[i]);
                    if ((++i) < len) {
                        stringBuilder.append(",");
                    }
                }
            }
            stringBuilder.append("],");
        }
        return stringBuilder.toString();
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        if (handler instanceof HandlerMethod) {
            SerialNo.clear();
        }
    }
}